package com.gjr.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gjr.commonutils.Result;
import com.gjr.eduservice.entity.EduSubject;
import com.gjr.eduservice.entity.excel.SubjectData;
import com.gjr.eduservice.listener.SubjectExcelListener;
import com.gjr.eduservice.mapper.EduSubjectMapper;
import com.gjr.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author gjr
 * @since 2021-12-08
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     *
     * @param file              文件
     * @param eduSubjectService 该service是为了能让监听器实现数据库操作
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) throws IOException {
        // 文件输入流
        InputStream in = file.getInputStream();
        // 调用方法进行读取
        EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
    }

    /**
     * 课程分类列表（树形结构）
     *
     * @return 一级和二级分类
     */
    @Override
    public List<Map<String, Object>> getAllSubject() {
        // 查询出所有一级分类
        List<EduSubject> oneList = this.list(new LambdaQueryWrapper<EduSubject>()
                .eq(EduSubject::getParentId, 0));

        // 一级分类为空时
        if (!CollectionUtils.isNotEmpty(oneList)) {
            return null;
        }

        return this.recursionSubject(oneList);
    }

    /**
     * 递归取数据
     * 递归结束条件：当 children 为空时。
     *
     * @param eduSubjectList 课程科目集合
     * @return 级别已封装好
     */
    private List<Map<String, Object>> recursionSubject(List<EduSubject> eduSubjectList) {
        List<Map<String, Object>> returnList = new LinkedList<>();

        // 遍历一级分类集合
        for (EduSubject eduSubject : eduSubjectList) {
            Map<String, Object> map = new LinkedHashMap<>();

            String id = eduSubject.getId();
            map.put("id", id);
            map.put("label", eduSubject.getTitle());

            // 查询父id中有没有引用一级分类的id，如有引用那就说明是某一级分类下面的
            List<EduSubject> list = this.list(new LambdaQueryWrapper<EduSubject>()
                    .eq(EduSubject::getParentId, id));

            // 添加到 children 键中
            if (CollectionUtils.isNotEmpty(list)) {
                map.put("children", recursionSubject(list));
            } else {
                map.put("children", null);
            }

            returnList.add(map);
        }

        return returnList;
    }
}
